﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(false)]
    public partial class create_certificate : BaseForm
    {
        public create_certificate()
            : base()
        {
        }

        public create_certificate(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbAuthorization.DataSource = Utility.authorizations(dbContext);
            cbAuthorization.DisplayMember = "Name";

        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("USE master;");
            sb.AppendLine();

            sb.AppendLine("CREATE CERTIFICATE [" + tbName.Text + "]");
            if (chAuthorization.Checked)
            {
                sb.AppendLine("AUTHORIZATION [" + cbAuthorization.Text + "]");
            }

            if (rbFromExisiting.Checked)
            {
                if (rbAssembly.Checked)
                {
                    sb.AppendLine("FROM ASSEMBLY [" + lbAssemblies.Text + "]");
                }
                else
                {
                    string line = "";

                    if (cbExecutable.Checked)
                    {
                        line += "FROM EXECUTABLE FILE = ";
                    }
                    else
                    {
                        line += "FROM FILE = ";
                    }

                    line += "'" + tbFileName.Text + "' ";

                    if (cbprivateKey.Checked)
                    {
                        line += " WITH PRIVATE KEY FILE = '" + tbPrivatekeyFile.Text + "' ";

                        if (tbDecryptPrivateKey.Text != string.Empty)
                        {
                            line += ", DECRYPTION BY PASSWORD = '" + tbDecryptPrivateKey.Text + "' ";
                        }

                        if (tbEncryprPrivateKey.Text != string.Empty)
                        {
                            line += ", ENCRYPTION BY PASSWORD = '" + tbEncryprPrivateKey.Text + "' ";
                        }
                    }

                    sb.AppendLine(line);
                }
            }
            else
            {
                if (tbEncryptPassword.Text != string.Empty)
                {
                    sb.AppendLine("ENCRYPTION BY PASSWORD = '" + tbEncryptPassword.Text + "' ");
                }

                sb.AppendLine("WITH SUBJECT = '" + tbSubject.Text + "' ");

                if (tbFrom.Text != string.Empty)
                {
                    sb.AppendLine(", START_DATE = '" + tbFrom.Text + "' ");
                }

                if (tbExpiry.Text != string.Empty)
                {
                    sb.AppendLine(", EXPIRY_DATE = '" + tbExpiry.Text + "' ");
                }
            }

            if (cbActive.Checked)
            {
                sb.AppendLine("ACTIVE FOR BEGIN_DIALOG = ON");
            }
            else
            {
                sb.AppendLine("ACTIVE FOR BEGIN_DIALOG = OFF");
            }

            if (tbBackup.Text != string.Empty)
            {
                sb.AppendLine(";");
                sb.AppendLine("BACKUP CERTIFICATE [" + tbName.Text + "] TO FILE = '" + tbBackup.Text + "';");

            }

            tbCommand.Text = sb.ToString();

        }

        private void EnableNewKey()
        {
            lblEncryptPassword.Enabled = true;
            tbEncryptPassword.Enabled = true;
            lblSubject.Enabled = true;
            tbSubject.Enabled = true;
            lblFrom.Enabled = true;
            tbFrom.Enabled = true;
            lblExpiry.Enabled = true;
            tbExpiry.Enabled = true;

            rbAssembly.Enabled = false;
            rbFromFile.Enabled = false;
        }

        private void DisableNewKey()
        {
            lblEncryptPassword.Enabled = false;
            tbEncryptPassword.Enabled = false;
            tbEncryptPassword.Text = "";
            lblSubject.Enabled = false;
            tbSubject.Enabled = false;
            tbSubject.Text = "";
            lblFrom.Enabled = false;
            tbFrom.Enabled = false;
            tbFrom.Text = "";
            lblExpiry.Enabled = false;
            tbExpiry.Enabled = false;
            tbExpiry.Text = "";

            rbAssembly.Enabled = true;
            rbFromFile.Enabled = true;
        }

        private void EnableFromAssembly()
        {
            lblAssembies.Enabled = true;
            lbAssemblies.Enabled = true;
        }

        private void DisableFromAssembly()
        {
            lblAssembies.Enabled = false;
            lbAssemblies.Enabled = false;
        }

        private void EnableFromFile()
        {
            cbExecutable.Enabled = true;
            lblFileNameExec.Enabled = true;
            tbFileName.Enabled = true;
            btnFilename.Enabled = true;

            cbprivateKey.Enabled = true;

            lblPathPrivateKey.Enabled = false;
            tbPrivatekeyFile.Enabled = false;
            btnprivateKey.Enabled = false;

            lblDecryptPrivateKey.Enabled = false;
            tbDecryptPrivateKey.Enabled = false;
            lblEcryptPrivateKey.Enabled = false;
            tbEncryprPrivateKey.Enabled = false;
        }

        private void DisableFromFile()
        {
            cbExecutable.Enabled = false;
            lblFileNameExec.Enabled = false;
            tbFileName.Enabled = false;
            btnFilename.Enabled = false;

            cbprivateKey.Enabled = false;

            lblPathPrivateKey.Enabled = false;
            tbPrivatekeyFile.Enabled = false;
            btnprivateKey.Enabled = false;

            lblDecryptPrivateKey.Enabled = false;
            tbDecryptPrivateKey.Enabled = false;
            lblEcryptPrivateKey.Enabled = false;
            tbEncryprPrivateKey.Enabled = false;
        }

        private void EnablePrivateKey()
        {
            lblPathPrivateKey.Enabled = true;
            tbPrivatekeyFile.Enabled = true;
            btnprivateKey.Enabled = true;

            lblDecryptPrivateKey.Enabled = true;
            tbDecryptPrivateKey.Enabled = true;
            lblEcryptPrivateKey.Enabled = true;
            tbEncryprPrivateKey.Enabled = true;
        }

        private void DisableprivateKey()
        {
            lblPathPrivateKey.Enabled = false;
            tbPrivatekeyFile.Enabled = false;
            btnprivateKey.Enabled = false;

            lblDecryptPrivateKey.Enabled = false;
            tbDecryptPrivateKey.Enabled = false;
            lblEcryptPrivateKey.Enabled = false;
            tbEncryprPrivateKey.Enabled = false;
        }

        private void rbNewKey_CheckedChanged(object sender, EventArgs e)
        {
            if (rbNewKey.Checked)
            {
                EnableNewKey();
                DisableFromFile();
                DisableFromAssembly();
            }
            else
            {
                DisableNewKey();

                if (rbFromFile.Checked)
                {
                    EnableFromFile();
                    DisableFromAssembly();
                }
                else
                {
                    EnableFromAssembly();
                    DisableFromFile();
                }
            }

            Write();
        }

        private void rbFromExisiting_CheckedChanged(object sender, EventArgs e)
        {
            if (rbFromExisiting.Checked)
            {
                if (rbFromFile.Checked)
                {
                    EnableFromFile();
                    DisableFromAssembly();
                }
                else
                {
                    DisableFromFile();
                    EnableFromAssembly();
                }
            }
            else
            {
                EnableNewKey();
                DisableFromFile();
                DisableFromAssembly();
            }
            Write();
        }

        private void rbAssembly_CheckedChanged(object sender, EventArgs e)
        {
            if (rbAssembly.Checked)
            {
                EnableFromAssembly();
                DisableFromFile();
            }
            else
            {
                DisableFromAssembly();
                EnableFromFile();
            }
            Write();
        }

        private void rbFromFile_CheckedChanged(object sender, EventArgs e)
        {
            if (rbFromFile.Checked)
            {
                EnableFromFile();
                DisableFromAssembly();
            }
            else
            {
                DisableFromFile();
                EnableFromAssembly();
            }
            Write();

        }

        private void cbprivateKey_CheckedChanged(object sender, EventArgs e)
        {
            if (cbprivateKey.Checked)
            {
                EnablePrivateKey();
            }
            else
            {
                DisableprivateKey();
            }
            Write();
        }

        private void tbFileName_TextChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void tbPrivatekeyFile_TextChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void tbDecryptPrivateKey_TextChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void tbEncryprPrivateKey_TextChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void tbEncryptPassword_TextChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void tbSubject_TextChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void tbFrom_TextChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void tbExpiry_TextChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void cbActive_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void chAuthorization_CheckedChanged(object sender, EventArgs e)
        {
            cbAuthorization.Enabled = chAuthorization.Checked;

            Write();
        }

        private void cbAuthorization_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void btnFilename_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.AddExtension = true;
            dialog.CheckFileExists = true;
            dialog.CheckPathExists = true;
            dialog.DefaultExt = ".cer";
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tbFileName.Text = dialog.FileName;
            }

            Write();
        }

        private void btnprivateKey_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.AddExtension = true;
            dialog.CheckFileExists = true;
            dialog.CheckPathExists = true;
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tbPrivatekeyFile.Text = dialog.FileName;
            }

            Write();

        }

        private void cbExecutable_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbBackup_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.AddExtension = true;
            dialog.CheckPathExists = true;
            dialog.DefaultExt = ".cer";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tbBackup.Text = dialog.FileName;
            }


        }
    }
}